
class Dep {
    constructor() {
        this.adds = []
    }
    addSub(watcher) {
        this.adds.push(watcher)
    }
    notify() {
        this.adds.forEach(v => {
            v.update()
        })
    }
}
class Watcher {
    constructor(vm, key, cb) {
        this.vm = vm;
        (this.key = key);
        (this.cb = cb);
        Dep.target = this;
        key.split(".").reduce((obj, k) => obj[k], vm);
        Dep.target = null;
    }
    update() {
        const value = this.key.split(".").reduce((obj, k) => obj[k], this.vm);
        this.cb(value);
    }
}

class Watch {
    constructor() {
        this.watchs = []
    }
    addWatch(vm, watchs, key) {
        this.addWatchProps({
            key,
            fn: watchs[key].bind(vm)
        })
    }
    addWatchProps(watchProps) {
        this.watchs.push(watchProps)
    }
    invoke(key, newVal, oldVal) {
        this.watchs.forEach(v => {
            if (v.key === key) {
                v.fn(newVal, oldVal)
            }
        })
    }
}